<!--
 * @Description: json生成表单模块，表单验证
 * @Author: Boide
 * @Date: 2021-09-06 18:00:01
 * @LastEditors: guiyi@hongyi.tech
 * @LastEditTime: 2021-09-13 17:34:29
-->
<template>
  <div>
    <el-form label-width="100px">
      <el-row>
      <template v-for="(item, i) in formConf">
        <el-col :key="i" :span="12*(item.cell||1)">
          <el-form-item
            v-if="item.model!=='id'"
            :prop="item.model"
            :label='item.showName+":"'
          >
            <!-- 时间 -->
            <div v-if="item.ftype==='dateTime'">{{formModel[item.model] | parseTime}}</div>
            <!-- 富文本 -->
            <template v-else-if="item.ftype==='richText'">
              <div v-html="formModel[item.model]"></div>
            </template>
            <!-- 颜色 -->
            <template v-else-if="item.ftype==='color'">
              <div class="color-box" :style="{'background': formModel[item.model]}"></div>
            </template>
            <!-- 图片 -->
            <template v-else-if="item.ftype==='image'">
              <upload-img
                v-if="formModel[item.model]"
                :value="formModel[item.model].split('@!#!@')"
                :multiple="formModel[item.model].indexOf('@!#!@')!==-1"
                :disabled="true"
                :show="true"
              />
            </template>
            <!-- 文件 -->
            <template v-else-if="item.ftype==='file'">
              <div>{{!!formModel[item.model]&&formModel[item.model].split(`@=#=@`)[0]||''}}</div>
            </template>
            <!-- 其他 -->
            <div style="white-space: pre-wrap;" v-else>{{formModel[item.model]}}</div>
          </el-form-item>
        </el-col>
      </template>
      </el-row>
      <el-form-item label=' '>
        <el-button type="primary" @click="cancel">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import UploadImg from '../upload-img'
export default {
  name: 'hy-form-detail',
  components: { UploadImg },
  props: {
    formData: {
      type: Array,
      default() {
        return []
      }
    },
    value: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    if (!this.formData.length) return { formConf: [] }
    const data = {
      formConf: JSON.parse(JSON.stringify(this.formData)),
      formModel: {}
    }
    this.initFormModel(data.formConf, data.formModel)
    return data
  },
  watch: {
    value(newVal) {
      this.formModel = newVal
    }
  },
  methods: {
    initFormModel(componentList = [], formModel) {
      componentList.forEach(cur => {
        if (cur.model) {
          formModel[cur.model] = cur.initValue || ''
        }
      })
    },
    // 取消
    cancel() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>

<style lang="scss" >
.color-box{
  display: flex;
  width: 32px;
  height: 32px;
}
</style>
